import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {STORAGE_KEY} from "../app.general";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor() {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the auth header from the service.
    let authHeader:string = localStorage.getItem(STORAGE_KEY.token);
    // Clone the request to add the new header.
    console.log(authHeader + 'header===');
    let authReq: any;
    if (!authHeader || authHeader === 'undefined')
      authReq = req.clone();
    else
      authReq = req.clone({headers: req.headers.set('Authorization', 'Bearer ' + authHeader)});
    // Pass on the cloned request instead of the original request.
    return next.handle(authReq);
  }


}
